package com.litchishell.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LitchiShell
 * @description: 题目13：罗马数字转整数
 * @create 2022-08-17
 */
public class Solution003 {

    /**
     * 常规解法
     * @param s
     * @return
     */
    public static int romanToInt(String s) {
        int length = s.length();
        if (length == 0) {
            return -1;
        }
        Map<String, Integer> valueMap = new HashMap<>(13);
        valueMap.put("I", 1);
        valueMap.put("V", 5);
        valueMap.put("X", 10);
        valueMap.put("L", 50);
        valueMap.put("C", 100);
        valueMap.put("D", 500);
        valueMap.put("M", 1000);
        valueMap.put("IV", 4);
        valueMap.put("IX", 9);
        valueMap.put("XL", 40);
        valueMap.put("XC", 90);
        valueMap.put("CD", 400);
        valueMap.put("CM", 900);
        if (length == 1) {
            return valueMap.get(s);
        }
        if (length == 2) {
            Integer integer = valueMap.get(s);
            if (integer == null) {
                return valueMap.get(s.substring(0,1)) + valueMap.get(s.substring(1));
            }
            return integer;
        }
        int sum = 0;
        int changeLength = length;
        if (s.contains("XL")) {
            s = s.replaceAll("XL", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("XL");
            changeLength = s.length();
        }
        if (s.contains("XC")) {
            s = s.replaceAll("XC", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("XC");
            changeLength = s.length();
        }
        if (s.contains("CD")) {
            s = s.replaceAll("CD", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("CD");
            changeLength = s.length();
        }
        if (s.contains("CM")) {
            s = s.replaceAll("CM", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("CM");
            changeLength = s.length();
        }
        if (s.contains("IV")) {
            s = s.replaceAll("IV", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("IV");
            changeLength = s.length();
        }
        if (s.contains("IX")) {
            s = s.replaceAll("IX", "");
            sum = sum + (changeLength - s.length()) / 2 * valueMap.get("IX");
            changeLength = s.length();
        }
        for (int i = 0; i < changeLength - 1; i++) {
            sum = sum + valueMap.get(s.substring(i, i + 1));
        }
        if (changeLength > 0) {
            sum = sum + valueMap.get(s.substring(changeLength - 1));
        }
        return sum;
    }
    Map<Character, Integer> symbolValues = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};

    public int romanToIntCopy(String s) {
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int value = symbolValues.get(s.charAt(i));
            if (i < n - 1 && value < symbolValues.get(s.charAt(i + 1))) {
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("XLIV"));
    }
}
